<template>
  <div class="dashboard-container">
    <div class="main-content">
      <div class="content-wrapper">
        <div class="section-title">存储概览</div>
        <div class="storage-info">
          <div class="storage-circle">
            <svg class="progress-ring" width="180" height="180">
              <defs>
                <linearGradient id="gradient" x1="0%" y1="0%" x2="100%" y2="0%">
                  <stop offset="0%" stop-color="#00c6fb" />
                  <stop offset="100%" stop-color="#005bea" />
                </linearGradient>
                <filter id="glow">
                  <feGaussianBlur stdDeviation="3" result="coloredBlur"/>
                  <feMerge>
                    <feMergeNode in="coloredBlur"/>
                    <feMergeNode in="SourceGraphic"/>
                  </feMerge>
                </filter>
              </defs>
              <circle
                  class="progress-ring-circle-bg"
                  cx="90"
                  cy="90"
                  r="80"
                  stroke="#e8f0fe"
                  stroke-width="10"
                  fill="none"
              />
              <circle
                  class="progress-ring-circle"
                  cx="90"
                  cy="90"
                  r="80"
                  stroke="url(#gradient)"
                  stroke-width="10"
                  stroke-linecap="round"
                  fill="none"
                  transform="rotate(-90 90 90)"
                  :stroke-dasharray="circumference"
                  :stroke-dashoffset="strokeOffset"
                  filter="url(#glow)"
              />
            </svg>
            <div class="storage-text">
              <div class="percentage">{{ storageData.percentage.toFixed(2) }}<span>%</span></div>
              <div class="used-info">已使用空间</div>
              <div class="total-info">
                <span class="used">{{ storageData.used.toFixed(2) }}GB</span>
                <span class="separator">/</span>
                <span class="total">{{ storageData.total.toFixed(2) }}GB</span>
              </div>
            </div>
          </div>
        </div>

        <div class="section-title">文件分类</div>
        <div class="file-categories">
          <div class="category-item" v-for="(category, index) in fileCategories" :key="index">
            <div class="category-header">
              <div class="icon" :class="category.type">
                <span class="icon-text">{{ category.iconText }}</span>
              </div>
              <div class="size">{{ category.size }}</div>
            </div>
            <div class="category-content">
              <div class="name">{{ category.name }}</div>
              <div class="update-info">
                <div class="upload-time">上次更新</div>
                <div class="date">{{ category.lastUpdate }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="sidebar-content">
      <div class="contacts-section">
        <div class="contacts-header">
          <h3>联系人</h3>
          <div class="add-contact">
            <span class="icon-text">+</span>
          </div>
        </div>
        <div class="contact-list">
          <div class="contact-item" v-for="(contact, index) in contacts" :key="index">
            <div class="contact-avatar">
              <img :src="contact.avatar" alt="联系人头像">
            </div>
            <div class="contact-info">
              <div class="contact-name">{{contact.name}}</div>
              <div class="contact-email">{{contact.email}}</div>
            </div>
          </div>
        </div>
      </div>

      <div class="invite-section">
        <h3>邀请成员</h3>
        <div class="invite-input">
          <input type="text" value="HKPD98HUOGTH12" readonly>
          <button>复制</button>
        </div>
        <div class="share-icons">
          <div class="share-icon">
            <span class="icon-text">💬</span>
          </div>
          <div class="share-icon">
            <span class="icon-text">✉️</span>
          </div>
          <div class="share-icon">
            <span class="icon-text">↗️</span>
          </div>
          <div class="share-icon">
            <span class="icon-text">🔗</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref, onMounted, computed, onBeforeUnmount} from 'vue';

// 文件分类数据
const fileCategories = ref([]);
const isFileLoading = ref(true);
const fileError = ref(null);


// 存储数据响应式对象
const storageData = ref({
  percentage: 0,
  used: 0,
  total: 128 // 初始值（会被接口数据覆盖）
});

// 进度条计算属性
const circumference = 2 * Math.PI * 80; // 周长计算
const strokeOffset = computed(() =>
    circumference * (1 - storageData.value.percentage / 100)

);

// 获取存储信息
const fetchStorageInfo = async () => {
  try {
    const response = await fetch('http://localhost:8080/api/resources/storage');
    if (!response.ok) throw new Error('请求失败');

    const data = await response.json();
    storageData.value = {
      percentage: parseFloat(data.percentage.toFixed(2)),
      used: parseFloat(data.used.toFixed(2)),
      total: parseFloat(data.total.toFixed(2))
    };
  } catch (error) {
    console.error('获取存储信息失败:', error);
    // 可以添加UI错误提示
  }
};

// 组件挂载时获取数据
onMounted(() => {
  fetchStorageInfo();
  // 可选：定时刷新数据（每5分钟）
  setInterval(fetchStorageInfo, 300000);
});

// 文件分类数据
const fetchFileCategories = async () => {
  try {
    fileError.value = null;
    isFileLoading.value = true;

    const response = await fetch('http://localhost:8080/api/resources/file-categories');
    if (!response.ok) throw new Error(`请求失败 ${response.status}`);

    const data = await response.json();

    // 确保 data 包含预期字段
    const documentData = data.document || {};
    const imageData = data.image || {};
    const videoData = data.video || {};
    const otherData = data.other || {};

    // 转换数据格式，提供默认值
    fileCategories.value = [
      {
        type: 'document',
        iconText: '📄',
        name: '文件',
        size: documentData.size ? parseFloat(documentData.size.toFixed(2)) : 0,
        lastUpdate: documentData.lastModified || 'N/A'
      },
      {
        type: 'image',
        iconText: '🖼️',
        name: '图像',
        size: imageData.size ? parseFloat(imageData.size.toFixed(2)) : 0,
        lastUpdate: imageData.lastModified || 'N/A'
      },
      {
        type: 'video',
        iconText: '🎬',
        name: '视频、音频',
        size: videoData.size ? parseFloat(videoData.size.toFixed(2)) : 0,
        lastUpdate: videoData.lastModified || 'N/A'
      },
      {
        type: 'other',
        iconText: '📁',
        name: '其他',
        size: otherData.size ? parseFloat(otherData.size.toFixed(2)) : 0,
        lastUpdate: otherData.lastModified || 'N/A'
      }
    ];

  } catch (err) {
    console.error('获取文件分类失败:', err);
    fileError.value = `文件分类加载失败: ${err.message}`;
  } finally {
    isFileLoading.value = false;
  }
};

// 组件挂载时获取数据
onMounted(() => {
  fetchStorageInfo();
  fetchFileCategories();

  // 定时刷新数据（每5分钟）
  const interval = setInterval(() => {
    fetchStorageInfo();
    fetchFileCategories();
  }, 300000);

  onBeforeUnmount(() => clearInterval(interval));
});

// 联系人数据
const contacts = ref([
  { name: '有料素材', email: 'youliaosuicai@163.com', avatar: 'https://via.placeholder.com/40' },
  { name: 'Anne公主', email: 'anne.princess@hotmail.com', avatar: 'https://via.placeholder.com/40' },
  { name: '有什么事', email: 'math990909@hotmail.com', avatar: 'https://via.placeholder.com/40' },
  { name: '周四丰', email: 'henry.mechanical1234@gmail.com', avatar: 'https://via.placeholder.com/40' },
  { name: 'Geogore Michael', email: 'george7134@gmail.com', avatar: 'https://via.placeholder.com/40' },
  { name: '罗伯特·明尼', email: 'luo.robert23@hotmail.com', avatar: 'https://via.placeholder.com/40' }
]);
</script>

<style scoped>
.file-loading {
  position: relative;
  min-height: 300px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 20px auto;
}

.file-error {
  padding: 15px;
  background: #ffe3e3;
  color: #ff4444;
  border-radius: 8px;
  margin: 20px 0;
  border: 1px solid #ffaaaa;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.dashboard-container {
  display: flex;
  gap: 20px;
  height: calc(100vh - 100px);
  padding: 0;
  margin: 0;
  overflow: hidden;
}

.main-content {
  flex: 6;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 20px;
}

.content-wrapper {
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 15px;
  padding-left: 10px;
  border-left: 3px solid #6c8cff;
}

.sidebar-content {
  flex: 4;
  display: flex;
  flex-direction: column;
  gap: 20px;
  height: 100%;
  overflow-y: auto;
  padding-right: 10px;
}

.storage-info {
  background: linear-gradient(135deg, #f6f9ff, #f0f5ff);
  border-radius: 24px;
  padding: 40px;
  color: #2c5282;
  margin-bottom: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 0 15px 30px rgba(0, 198, 251, 0.1);
  position: relative;
  overflow: hidden;
  min-height: 260px;
  border: 1px solid rgba(0, 198, 251, 0.1);
}

.storage-info::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(
      circle,
      rgba(0, 198, 251, 0.08) 0%,
      rgba(0, 91, 234, 0.05) 30%,
      rgba(255, 255, 255, 0) 70%
  );
  animation: rotate 20s linear infinite;
}

.storage-circle {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1;
}

.progress-ring {
  transform: rotate(-90deg);
  filter: drop-shadow(0 6px 12px rgba(0, 198, 251, 0.2));
}

.progress-ring-circle-bg {
  opacity: 0.15;
  transition: all 0.5s ease;
}

.progress-ring-circle {
  transition: stroke-dashoffset 1.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.storage-text {
  position: absolute;
  text-align: center;
  transform: translateY(-5px);
}

.percentage {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 5px;
  background: linear-gradient(to bottom, #2c5282, #2b6cb0);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: pulse 2s ease-in-out infinite;
  letter-spacing: -1px;
}

.percentage span {
  font-size: 24px;
  font-weight: 500;
  margin-left: 2px;
  opacity: 0.8;
}

.used-info {
  font-size: 15px;
  opacity: 0.9;
  margin-bottom: 8px;
  font-weight: 500;
  letter-spacing: 0.5px;
  color: #2d3748;
}

.total-info {
  font-size: 14px;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 4px;
}

.total-info .used {
  color: #00c6fb;
  font-weight: 600;
}

.total-info .separator {
  color: #718096;
  opacity: 0.7;
  margin: 0 2px;
}

.total-info .total {
  color: #718096;
  opacity: 0.8;
}

.file-categories {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 20px;
  flex: 1;
  margin-bottom: 30px;
}

.category-item {
  background-color: #fff;
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
  overflow: hidden;
}

.category-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  right: 0;
  width: 60px;
  height: 60px;
  background-color: rgba(0, 0, 0, 0.02);
  border-radius: 50% 0 0 0;
}

.category-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}

.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.icon {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  position: relative;
  z-index: 1;
}

.icon::before {
  content: '';
  position: absolute;
  top: -5px;
  left: -5px;
  right: -5px;
  bottom: -5px;
  background: inherit;
  border-radius: 16px;
  filter: blur(8px);
  opacity: 0.6;
  z-index: -1;
}

.icon-text {
  font-size: 24px;
}

.size {
  font-size: 22px;
  font-weight: bold;
  color: #333;
  position: relative;
}

.size::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, transparent, currentColor, transparent);
  opacity: 0.2;
}

.category-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.name {
  font-size: 16px;
  font-weight: 500;
  color: #555;
  margin-bottom: 15px;
}

.update-info {
  margin-top: auto;
  border-top: 1px dashed #eee;
  padding-top: 10px;
  position: relative;
}

.update-info::before {
  content: '⏱️';
  position: absolute;
  top: 10px;
  right: 0;
  font-size: 12px;
  opacity: 0.5;
}

.upload-time {
  font-size: 12px;
  color: #999;
  margin-bottom: 3px;
}

.date {
  font-size: 13px;
  color: #666;
}

.icon.document {
  background: linear-gradient(135deg, #ff7b7b, #e65a5a);
  box-shadow: 0 3px 8px rgba(255, 123, 123, 0.3);
}

.icon.image {
  background: linear-gradient(135deg, #7ba4ff, #5a78e6);
  box-shadow: 0 3px 8px rgba(123, 164, 255, 0.3);
}

.icon.video {
  background: linear-gradient(135deg, #7bffb7, #5ae68a);
  box-shadow: 0 3px 8px rgba(123, 255, 183, 0.3);
}

.icon.other {
  background: linear-gradient(135deg, #d67bff, #b45ae6);
  box-shadow: 0 3px 8px rgba(214, 123, 255, 0.3);
}

.contacts-section {
  background-color: #fff;
  border-radius: 15px;
  padding: 20px;
  flex: 1;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  min-height: 200px;
}

.contacts-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.contacts-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.add-contact {
  width: 32px;
  height: 32px;
  background-color: #f5f5f5;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.2s;
}

.add-contact:hover {
  background-color: #e0e0e0;
}

.contact-list {
  overflow-y: auto;
  flex: 1;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f5f5f5;
  transition: background-color 0.2s;
}

.contact-item:hover {
  background-color: #f9f9f9;
}

.contact-item:last-child {
  border-bottom: none;
}

.contact-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 12px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.contact-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.contact-name {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 3px;
}

.contact-email {
  font-size: 12px;
  color: #999;
}

.invite-section {
  background-color: #fff;
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
  height: 200px;
  display: flex;
  flex-direction: column;
}

.invite-section h3 {
  margin-top: 0;
  margin-bottom: 15px;
  font-size: 18px;
  font-weight: 600;
}

.invite-input {
  display: flex;
  margin-bottom: 20px;
}

.invite-input input {
  flex: 1;
  border: 1px solid #eee;
  border-radius: 8px 0 0 8px;
  padding: 10px 15px;
  outline: none;
  font-family: monospace;
  letter-spacing: 1px;
}

.invite-input button {
  background: linear-gradient(135deg, #6c8cff, #5a78e6);
  color: white;
  border: none;
  border-radius: 0 8px 8px 0;
  padding: 0 20px;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.2s;
}

.invite-input button:hover {
  background: linear-gradient(135deg, #5a78e6, #4967d5);
}

.share-icons {
  display: flex;
  justify-content: space-between;
  margin-top: auto;
}

.share-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.2s, background-color 0.2s;
}

.share-icon:hover {
  background-color: #e0e0e0;
  transform: scale(1.1);
}

@media (max-width: 1200px) {
  .dashboard-container {
    flex-direction: column;
    height: auto;
  }

  .main-content {
    height: auto;
  }

  .sidebar-content {
    flex-direction: row;
    height: auto;
  }

  .contacts-section, .invite-section {
    flex: 1;
    height: 300px;
  }

  .file-categories {
    height: auto;
  }
}

@media (max-width: 768px) {
  .sidebar-content {
    flex-direction: column;
  }

  .file-categories {
    grid-template-columns: 1fr;
  }

  .contacts-section, .invite-section {
    height: auto;
  }

  .storage-info {
    padding: 25px;
    min-height: 220px;
  }

  .progress-ring {
    width: 140px;
    height: 140px;
  }

  .progress-ring circle {
    cx: 70;
    cy: 70;
    r: 62;
    stroke-width: 8;
  }

  .percentage {
    font-size: 36px;
  }

  .percentage span {
    font-size: 18px;
  }

  .used-info {
    font-size: 13px;
    margin-bottom: 6px;
  }

  .total-info {
    font-size: 12px;
  }
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse {
  0% {
    opacity: 0.8;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.02);
  }
  100% {
    opacity: 0.8;
    transform: scale(1);
  }
}
</style>